﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;

namespace WebAssistant.Common
{
    public class StringHelper
    {
        /// <summary>
        /// 替换字符串
        /// </summary>
        /// <param name="Char1">被替换的字符串</param>
        /// <param name="Char2">替换的字符串</param>
        /// <param name="String"></param>
        /// <returns>返回替换后的字符串</returns>
        public static string ReplaceChar(string Char1,string Char2, string String)
        {
            if (String.Contains(Char1))
            {
                String = String.Replace(Char1, Char2);
            }
            
            return String;
        }

        #region 返回字符在字符串中出现的次数
        /// <summary>
        /// 返回字符在字符串中出现的次数
        /// </summary>
        /// <param name="Char">要检测出现的字符</param>
        /// <param name="String">要检测的字符串</param>
        /// <returns>出现次数</returns>
        public static int GetCharInStringCount(string Char, string String)
        {
            string str = String.Replace(Char, "");
            return (String.Length - str.Length) / Char.Length;

        }
        #endregion

        #region 截取字符串
        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="str_value"></param>
        /// <param name="str_len"></param>
        /// <returns></returns>
        public static string leftx(string str_value, int str_len)
        {
            int p_num = 0;
            int i;
            string New_Str_value = "";

            if (str_value == "")
            {
                New_Str_value = "";
            }
            else
            {
                int Len_Num = str_value.Length;
                for (i = 0; i <= Len_Num - 1; i++)
                {
                    if (i > Len_Num) break;
                    char c = Convert.ToChar(str_value.Substring(i, 1));
                    if (((int)c > 255) || ((int)c < 0))
                        p_num = p_num + 2;
                    else
                        p_num = p_num + 1;



                    if (p_num >= str_len)
                    {

                        New_Str_value = str_value.Substring(0, i + 1);
                        break;
                    }
                    else
                    {
                        New_Str_value = str_value;
                    }

                }

            }
            return New_Str_value;
        }
        #endregion

        /// <summary>
        /// 截取2个字符字符之间的字符串
        /// </summary>
        /// <param name="sourse"></param>
        /// <param name="startstr"></param>
        /// <param name="endstr"></param>
        /// <returns></returns>
        public static string MidStrEx(string sourse, string startstr, string endstr)
        {
            string result = string.Empty;
            int startindex, endindex;
            try
            {
                startindex = sourse.IndexOf(startstr);
                if (startindex == -1)
                    return result;
                string tmpstr = sourse.Substring(startindex + startstr.Length);
                endindex = tmpstr.IndexOf(endstr);
                if (endindex == -1)
                    return result;
                result = tmpstr.Remove(endindex);
            }
            catch
            {
                
            }
            return result;
        }

        /// <summary>
        /// 字符串截取补字符函数
        /// </summary>
        /// <param name="s">要处理的字符串</param>
        /// <param name="len">长度</param>
        /// <param name="b">补充的字符</param>
        /// <returns>处理后字符</returns>
        public static string splitStringLen(string s, int len, char b)
        {
            if (string.IsNullOrEmpty(s))
                return "";
            if (s.Length >= len)
                return s.Substring(0, len);
            return s.PadRight(len, b);
        }

        /// <summary>
        /// 将网页图片转换成BASE64字符串
        /// </summary>
        /// <param name="url">图片URL</param>
        /// <returns></returns>
        public static string GetWebImageBase64String(string url)
        {
            WebClient mywebclient = new WebClient();
            byte[] Bytes = mywebclient.DownloadData(url);

            MemoryStream ms1 = new MemoryStream(Bytes);
            Image image = Image.FromStream(ms1);

            Bitmap bmp = new Bitmap(image);
            MemoryStream ms2 = new MemoryStream();
            bmp.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms2.Length];
            ms2.Position = 0;
            ms2.Read(arr, 0, (int)ms2.Length);
            ms2.Close();

            return Convert.ToBase64String(arr);
        }

        /// <summary>
        /// 替换字符串的换行符号
        /// </summary>
        /// <param name="ContentString"></param>
        /// <param name="ReplaceCharacter"></param>
        /// <returns></returns>
        public static string ReplaceStringWrap(string ContentString, string ReplaceCharacter)
        {
            bool hasWrap = ContentString.IndexOf("\n") != -1;
            if (hasWrap)
            {
                ContentString = ContentString.Replace("\n", ReplaceCharacter);
            }
            
            return ContentString;
        }
    }
}
